<?php
/**
 * /depot/library/mvc-helper/ActionHelper/ActionConfirm.php
 * @author qqinxl
 * 2008/03/31
 * 
 * @category  
 * @package  
 * @copyright  Copyright (c) {}
 */
class ActionHelper_ActionConfirm extends ActionHelper_ActionPost  {
	
	const FAIL_OLD_WRONG = 'fail_old_wrong';
	const FAIL_CONFIRM_WRONG = 'fail_confirm_wrong';	
	
	protected $_isPopulateFormData = false;
	
	protected $_field;
	protected $_field_old;
	protected $_field_confirm;
	
	const OPTIONS_CONFIRM_FIELD = 'confirm_field';
	
	public function setOptions($options){
		parent::setOptions($options);
		if(isset($options[self::OPTIONS_CONFIRM_FIELD])){
			$this->_field = $options[self::OPTIONS_CONFIRM_FIELD];
			$this->_field_old = 'old_'.$this->_field;
			$this->_field_confirm = 'confirm_'.$this->_field;
		}
	}
	
	public function init() {
		parent::init();
		//$module = $this->getActionController()->getRequest()->getModuleName();
		$controller = $this->getActionController()->getRequest()->getControllerName();
		$action = $this->getActionController()->getRequest()->getActionName();
		$str = $controller.'.'.$action;		
		$helperI18nMsg [self::FAIL_OLD_WRONG] = $str.'.fail.old.wrong';
		$helperI18nMsg [self::FAIL_CONFIRM_WRONG] = $str.'.fail.confirm.wrong';
		$this->_i18nMsg = array_merge($this->_i18nMsg,$helperI18nMsg);
	}
	
	protected function _doProcessBefore(){
		if ($this->_formData [$this->_field_old] != $this->_defaultData [$this->_field]) {
			$this->view->message = $this->_ ( $this->_i18nMsg[self::FAIL_OLD_WRONG]);
			$this->view->errors = array($this->_field_old=>'');
			//$this->getActionController()->getHelper('message')->set();
			return false;
		}elseif($this->_formData [$this->_field] != $this->_formData [$this->_field_confirm]){
			$this->view->message = $this->_ ( $this->_i18nMsg[self::FAIL_CONFIRM_WRONG]);
			$this->view->errors = array($this->_field=>'',$this->_field_confirm=>'');
			//$this->getActionController()->getHelper('message')->set();
			return false;
		} 
		return parent::_doProcessBefore();
	}	
}